package com.rankeiot.easy.vue.handler;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.HandlerInterceptor;



/**
 * 路径转换
 */

public class PathConvertHandler implements HandlerInterceptor {
    private String contentType;
    private String to;

    private int suffixLength=0;
    public PathConvertHandler(String contentType, String from, String to) {
        this.contentType = contentType;
        this.to = to;
        suffixLength=from.length();
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(HttpMethod.GET.matches(request.getMethod())){
            response.setContentType(contentType);
            String path=request.getRequestURI();
            request.getRequestDispatcher(path.substring(0,path.length()-suffixLength)+to)
                    .forward(request,response);
        }
        return true;
    }
}
